package com.living.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.living.R;
import com.md.basedpc.PHSizeUtils;
import com.md.livingwidget.uiutils.ShapeUtils;

import androidx.annotation.NonNull;

/**
 * 项目名称：com.yxt.function_sdk_share
 * 类名称：SharePopuWindow
 * 类描述：弹框
 * 创建人：wanhq
 * 创建时间：2019-12-03 19:11
 */
public class MorePopupWindow extends Dialog implements View.OnClickListener {

    private Object object;
    private Activity mActivity;
    private OnMoreItemClick onMoreItemClick;
    private View root_layout;
    private View menu_add;
    private View menu_download;
    private View menu_share;
    private View menu_delete;
    private boolean isMenu_add, isMenu_download, isMenu_share, isMenu_delete;

    public MorePopupWindow(@NonNull Context context, OnMoreItemClick onMoreItemClick) {
        super(context, R.style.custom_dialog2);
        this.mActivity = (Activity) context;
        this.onMoreItemClick = onMoreItemClick;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_player_list_more);
        initFullScreen();
        initView();
    }

    private void initFullScreen() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        window.getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    private void initView() {
        root_layout = findViewById(R.id.root_layout);
        root_layout.setBackground(ShapeUtils.getDrawableList(0, PHSizeUtils.mm2px(mActivity, 40), PHSizeUtils.mm2px(mActivity, 40)
                , 0, 0, 0, mActivity.getResources().getColor(R.color.white)));
        findViewById(R.id.btn_dialog_cancel).setOnClickListener(v -> dismiss());

        menu_add = findViewById(R.id.menu_add);
        menu_download = findViewById(R.id.menu_download);
        menu_share = findViewById(R.id.menu_share);
        menu_delete = findViewById(R.id.menu_delete);

        menu_add.setOnClickListener(this);
        menu_download.setOnClickListener(this);
        menu_share.setOnClickListener(this);
        menu_delete.setOnClickListener(this);

        menu_add.setVisibility(isMenu_add ? View.VISIBLE : View.GONE);
        menu_download.setVisibility(isMenu_download ? View.VISIBLE : View.GONE);
        // menu_share.setVisibility(isMenu_share ? View.VISIBLE : View.GONE);
        menu_share.setVisibility(View.GONE);
        menu_delete.setVisibility(isMenu_delete ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.menu_add) {
            if (onMoreItemClick != null) {
                onMoreItemClick.onClickAdd(object);
            }
        } else if (_id == R.id.menu_download) {
            if (onMoreItemClick != null) {
                onMoreItemClick.onClickDownload(object);
            }
        } else if (_id == R.id.menu_share) {
            if (onMoreItemClick != null) {
                onMoreItemClick.onClickShare(object);
            }
        } else if (_id == R.id.menu_delete) {
            if (onMoreItemClick != null) {
                onMoreItemClick.onClickDelete(object);
            }
        }
        dismiss();
    }

    public void setMenuShows(boolean isMenu_add, boolean isMenu_download, boolean isMenu_share, boolean isMenu_delete) {
        this.isMenu_add = isMenu_add;
        this.isMenu_download = isMenu_download;
        this.isMenu_share = isMenu_share;
        this.isMenu_delete = isMenu_delete;
    }

    public void showDialog(Object object) {
        this.object = object;
        show();
    }

    /***
     * popuwindow点击回调
     */
    public interface OnMoreItemClick {
        void onClickAdd(Object object);

        void onClickDownload(Object object);

        void onClickShare(Object object);

        void onClickDelete(Object object);
    }


}
